package com.bite.common.core.domain;

import com.bite.common.core.enums.ResultCode;
import lombok.Data;

@Data
public class R<T> {
    private int code; //定义一些固定的code，前后端约定好的
    private String msg; //code的辅助说明 一个code对应一个msg
    private T data; //请求某个接口返回的数据



    public static <T> R<T> ok() {
        return assembleResult(null, ResultCode.SUCCESS);
    }

    public static <T> R<T> ok(T data) {
        return assembleResult(data,ResultCode.SUCCESS);
    }

    public static <T> R<T> fail() {
        return assembleResult(null,ResultCode.FAILED);
    }
    public static <T> R<T> fail(int code, String msg) {
        return assembleResult(code,msg,null);
    }

    /**
     * 指定错误码
     */
    public static <T> R<T> fail(ResultCode resultCode) {
        return assembleResult(null, resultCode);
    }

    public static <T> R<T> assembleResult(T data, ResultCode resultCode) {
        R<T> result = new R<T>();
        result.setCode(resultCode.getCode());
        result.setMsg(resultCode.getMsg());
        result.setData(data);
        return result;
    }

    public static <T> R<T> assembleResult(int code, String msg, T data) {
        R<T> result = new R<T>();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

}
